<?php
	class TableData extends Element {
		private $data = "" ;
		public function __construct( $data = "" , $attributes = array() ){
			parent::__construct( "td" , $attributes );
			$this->data = $data ;
			$this->setValue( $this->data );
		}
		public function getCell( $data = "" , $attributes = array() ){
			return new TableData( $data );
		}
		public function setData( $data = "" ){ $this->setValue( $this->data = $data );}
		public function getData(){return $this->data ;}
	}
	class TableRow extends Element {
		private $cells = array();
		public function __construct( $datas = array() , $attributes=array() ){
			parent::__construct( "tr" , $attributes );
			$this->setCell($datas);
		}
		public function getRow( $datas = array() , $attributes=array() ){
			return new TableRow( $datas , $attributes );
		}
		public function setCell( $datas = array() ){
			if ( (is_array( $datas ) && !empty( $datas )) || is_object($datas) ){
				$this->createCells( $datas );
			}
		}
		private function createCells( $datas ){
			foreach( $datas AS $data ){
				$this->cells[] = new TableData($data);
			}
		} 
		public function addCell( $data = false , $index = false ){
			if ( $data !== false ){
				( $index === false || ( $index < 0 && $index > (count($this->cells)-1) ) ) ? $index = count($this->cells) : $index ;
				$tempCells = array();
				if ( $index == count( $this->cells ) ){
					$this->cells[] = new TableData($data);
				} else{
					foreach( $this->cells AS $index_ => $cell ){
						if ( $index_ == $index ){
							$tempCells[] = new TableData($data);
						}
						$tempCells[] = $this->cells[$index_];
					}
					$this->cells = $tempCells;
				}
			}
		}
		public function removeCell($index=false){
			if ( $index !== false && ( $index >= 0 && $index < count( $this->cells ) ) ){
				$tempCells = array();
				foreach( $this->cells AS $index_ => $cell ){
					if ( $index_ != $index ){
						$tempCells[] = $this->cells[$index_];
					}
				}
				$this->cells = $tempCells;
			}
		}
		public function getElement(){
			$cells = "" ;
			foreach ( $this->cells AS $cell ){
				$cells .= $cell->getElement() ;
			}
			$this->setValue( $cells );
			return parent::getElement();
		}
	}
	
	class Table extends Element {
		public function __construct( $attributes = array() ){
			parent::__construct( "table" , $attributes );
		}
		public function getTable($attributes=array()){
			return new Table($attributes);
		}
	}